package com._520it.crm.web.controller;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com._520it.crm.domain.Temporary;
import com._520it.crm.service.ITemporaryService;
import com._520it.crm.util.JsonResult;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class TemporaryController {
	@Autowired
	private ITemporaryService service;
	@RequestMapping("/temporary/list/view")
	public ModelAndView selectAll(ModelAndView modelAndView,
								  @RequestParam(defaultValue = "1") int current,
								  @RequestParam(defaultValue = "10") int size,
								  Long acceptId,
								  Long productId,
								  String applyNum,
								  String clientEleName,
								  @DateTimeFormat(pattern="yyyy-MM-dd") Date beginDate,
								  @DateTimeFormat(pattern="yyyy-MM-dd") Date endDate,
								  String plateNum
								  ){
		modelAndView.setViewName("temporary/list");
		List<Temporary> temporaryList = service.selectPage(current,size,acceptId,productId,applyNum,clientEleName,beginDate,endDate,plateNum);

		modelAndView.addObject("list",temporaryList);
		int count = service.selectPageCount(current,size,acceptId,productId,applyNum,clientEleName,beginDate,endDate,plateNum);
		int totalCount = count%size==0?count/size:count/size+1;
		modelAndView.addObject("totalCount",totalCount);
		modelAndView.addObject("current",current);
		return modelAndView;
	}
	
	@RequestMapping(value="/temporary/save",produces="application/json;charset=utf-8")
	@ResponseBody
	public Object save(Temporary temporary){
		service.insert(temporary);
		return JsonResult.createSuccess();
	}
	
	@RequestMapping("/temporary/index")
	public String temporaryIndex(){
		return "temporary/index";
	}
}
